package com.chuanye.jiguangimdemo.activitys;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chuanye.jiguangimdemo.R;
import com.chuanye.jiguangimdemo.app.MyApplication;
import com.chuanye.jiguangimdemo.model.InfoModel;

import java.io.File;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetUserInfoCallback;
import cn.jpush.im.android.api.model.UserInfo;

/**
 * 搜索添加好友
 */
public class SearchForAddFriendActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "SearchForAddFriendActivity";
    private Context mContext;
    private ImageView searchaddfriend_back_iv;
    private EditText searchaddfriend_content_et;
    private TextView searchaddfriend_seek_btn;
    private LinearLayout searchaddfriend_friend_ll;
    private ImageView searchaddfriend_friendicon_iv;
    private TextView searchaddfriend_username_tv;
    private Button searchaddfriend_add_btn;
    private TextView searchaddfriend_title_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_for_add_friend);
        mContext = this;

        initView();
    }

    @SuppressLint("WrongConstant")
    private void initView() {
        //返回
        searchaddfriend_back_iv = findViewById(R.id.searchaddfriend_back_iv);
        searchaddfriend_back_iv.setOnClickListener(this);
        //标题
        searchaddfriend_title_tv = findViewById(R.id.searchaddfriend_title_tv);
        //输入框
        searchaddfriend_content_et = findViewById(R.id.searchaddfriend_content_et);
        //搜索
        searchaddfriend_seek_btn = findViewById(R.id.searchaddfriend_seek_btn);
        searchaddfriend_seek_btn.setOnClickListener(this);

        //好友列表
        searchaddfriend_friend_ll = findViewById(R.id.searchaddfriend_friend_ll);
        searchaddfriend_friend_ll.setOnClickListener(this);
        //好友头像
        searchaddfriend_friendicon_iv = findViewById(R.id.searchaddfriend_friendicon_iv);
        //好友用户名
        searchaddfriend_username_tv = findViewById(R.id.searchaddfriend_username_tv);
        //加好友
        searchaddfriend_add_btn = findViewById(R.id.searchaddfriend_add_btn);
        searchaddfriend_add_btn.setOnClickListener(this);
        searchaddfriend_friend_ll.setVisibility(View.GONE);


        Intent intent = getIntent();
        if (intent.getFlags() == 2) {
            searchaddfriend_title_tv.setText("发起单聊");
            searchaddfriend_add_btn.setVisibility(View.GONE);
        } else {
            searchaddfriend_title_tv.setText("添加好友");
        }

    }

    @SuppressLint("WrongConstant")
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.searchaddfriend_back_iv://返回
                finish();
                break;
            case R.id.searchaddfriend_seek_btn://搜索
                String username = searchaddfriend_content_et.getText().toString().trim();
                if (TextUtils.isEmpty(username)){
                    Toast.makeText(mContext,"请输入用户名",Toast.LENGTH_SHORT).show();
                }else {
                    //获取用户信息
                    //异步从后台获取用户信息，此接口可用来获取不同appkey下用户的信息,如果appKey为空，则默认获取当前appkey下的用户信息。
                    JMessageClient.getUserInfo(username, "", new GetUserInfoCallback() {
                        @SuppressLint("LongLogTag")
                        @Override
                        public void gotResult(int responseCode, String responseMessage, UserInfo userInfo) {
                            Log.i(TAG,"获取用户信息: 返回码："+responseCode+" ,返回内容："+responseMessage);
                            if (responseCode == 0){//
                                InfoModel.getInstance().friendInfo  = userInfo;
                                if (userInfo != null){
                                    searchaddfriend_friend_ll.setVisibility(View.VISIBLE);
                                    if (userInfo.isFriend()){//如果已经是好友
                                        searchaddfriend_add_btn.setVisibility(View.GONE);
                                        //如果是发起单聊.那么不能显示加好友按钮
                                    }else if (!userInfo.isFriend() && getIntent().getFlags() != 2) {
                                        searchaddfriend_add_btn.setVisibility(View.VISIBLE);
                                    }/*else {
                                            searchaddfriend_add_btn.setVisibility(View.VISIBLE);
                                        }*/

                                    File avatarFile = userInfo.getAvatarFile();
                                    if (avatarFile != null){
                                        searchaddfriend_friendicon_iv.setImageBitmap(BitmapFactory.decodeFile(avatarFile.getAbsolutePath()));
                                        InfoModel.getInstance().setBitmap(BitmapFactory.decodeFile(avatarFile.getAbsolutePath()));
                                    }else {
                                        searchaddfriend_friendicon_iv.setImageResource(R.drawable.rc_default_portrait);
                                        InfoModel.getInstance().setBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.rc_default_portrait));
                                    }
                                    //设置搜索用户名
                                    searchaddfriend_username_tv.setText(TextUtils.isEmpty(userInfo.getNickname()) ? userInfo.getUserName() : userInfo.getNickname());
                                }


                            }else {
                                Toast.makeText(mContext,"用户不存在",Toast.LENGTH_SHORT).show();
                                searchaddfriend_friend_ll.setVisibility(View.GONE);
                            }

                        }
                    });
                }

                break;
            case R.id.searchaddfriend_add_btn://加好友 //添加申请
                Intent addintent = new Intent(mContext,VerificationActivity.class);
                startActivity(addintent);

                break;
            case R.id.searchaddfriend_friend_ll://查看好友详情
                Intent intent = new Intent();
                //详细资料
                if (InfoModel.getInstance().isFriend()) {
                    //已经是好友
                    intent.setClass(mContext, FriendInfoActivity.class);
                    intent.putExtra("addFriend", true);
                    intent.putExtra("targetId", InfoModel.getInstance().friendInfo.getUserName());
                    //直接发起单聊
                }else if (getIntent().getFlags() == 2){
                    intent.setClass(SearchForAddFriendActivity.this, GroupNotFriendActivity.class);
                    intent.putExtra(MyApplication.TARGET_ID, InfoModel.getInstance().friendInfo.getUserName());
                    intent.putExtra(MyApplication.TARGET_APP_KEY, InfoModel.getInstance().friendInfo.getAppKey());
                } else {
                    //添加好友
                    intent.setClass(mContext, SearchFriendInfoActivity.class);
                }
                startActivity(intent);

                break;
        }
    }
}
